﻿using System.Text;
using AspNetCoreWeb.Config;
using AspNetCoreWeb.Infrastructure.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Web;


namespace AspNetCoreWeb
{
    public class Startup
    {
        private readonly IConfiguration _configuration;
        private readonly IHostingEnvironment _hostingEnvironment;
        public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
        {
            this._configuration = configuration;
            this._hostingEnvironment = hostingEnvironment;
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            //使用的ASP.Net Core 中内置了日志系统，
            /*
            services.AddLogging(builder =>
            {
                builder
               .AddConfiguration(Configuration.GetSection("Logging"))
               .AddFilter("Microsoft", LogLevel.Warning)
               .AddConsole();
            });
           */
            //services.AddDbContextPool<SwObjectContext>(optionsBuilder =>
            //{
            //    optionsBuilder.UseSqlServerWithLazyLoading(services);
            //});
            var path = _hostingEnvironment.ContentRootPath;
            services.AddSingleton(ConfigureStartupConfig<MySettings>(_configuration.GetSection("Mysetting")));
            var defaultcon = _configuration.GetSection("Mysetting").GetConnectionString("DefaultConnection");
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            //控制 乱码 
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //使用nlog
            loggerFactory.AddNLog();
            env.ConfigureNLog("nlog.config");
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

        public TConfig ConfigureStartupConfig<TConfig>(IConfiguration configuration) where TConfig : class, new()
        {
            var config = new TConfig();
            configuration.Bind(config);
            return config;
        }
    }
}
